/*oOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoO*/
/*
This files contains functions declarations related to server of HW assignment 1. 

Created by Amir Balva for the course "Introduction To Systems Programming", 
School of Electrical Engineering, Tel-Aviv University. winter 2014.
*/
/*oOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoO*/

#define _CRT_SECURE_NO_WARNINGS
#include "HW1_Server.h"
#include <string.h>
#include <Windows.h> // for Sleep()

#define NUM_OF_SERVER_ACCESSES 4
#define NUM_OF_EMAILS 13
#define ARRAY1_SIZE 5
#define ARRAY2_SIZE 1
#define ARRAY3_SIZE 7

static Email *emails_array1;
static Email *emails_array2;
static Email *emails_array3;

static int current_access;


Email * Create_New_Email ()
{
	return (Email*)malloc(sizeof(Email));
}


void Initilize_Server(const char * SetupString)
{
	//Ram 8_11_14 test entrance
	//printf("in module: %s function: %s, input is: %s\r\n", "HW1_Server.c", "Initilize_Server", SetupString);
	//
	Email * email;
	current_access=0;

	emails_array1=(Email*)malloc(ARRAY1_SIZE*sizeof(Email));
	emails_array2=(Email*)malloc(ARRAY2_SIZE*sizeof(Email));
	emails_array3=(Email*)malloc(ARRAY3_SIZE*sizeof(Email));


//Email 1

	email=&emails_array1[0];
	email->isRead=F;
	email->isMarked=F;
	email->ID=1;
	email->hour=235959;
	email->date=20141103;
	strcpy(email->recipient,"lecturer@tau.ac.il");
	strcpy(email->sender,"student@tau.ac.il");
	strcpy(email->subject,"Extension");
	strcpy(email->content,"Hi lecturer, can I please get an extension to HW1?\nThanks,\nStudent");

	//Email 2

	email=&emails_array1[1];
	email->isRead=F;
	email->isMarked=F;
	email->ID=2;
	email->hour=142015;
	email->date=20141108;
	strcpy(email->recipient,"student@tau.ac.il");
	strcpy(email->sender,"lecturer@tau.ac.il");
	strcpy(email->subject,"Re: Extension");
	strcpy(email->content,"No.");
	
	//Email 3

	email=&emails_array1[2];
	email->isRead=F;
	email->isMarked=F;
	email->ID=21;
	email->hour=234411;
	email->date=20131101;
	strcpy(email->recipient,"kushkuson@tau.ac.il");
	strcpy(email->sender,"student@tau.ac.il");
	strcpy(email->subject,"phish");
	strcpy(email->content,"Hi lecturer, you good very teacher?\nThanks you,\nStudent");
	
	
	//Email 4

	email=&emails_array1[3];
	email->isRead=F;
	email->isMarked=F;
	email->ID=16;
	email->hour=21728;
	email->date=15921403;
	strcpy(email->recipient,"shop@tau.ac.il");
	strcpy(email->sender,"client@tau.ac.il");
	strcpy(email->subject,"Abomination");
	strcpy(email->content,"It has been two weeks now since I've ordered your product and it is yet to arrive.\n Please explain this flagrancy.");
	
	//Email 5

	email=&emails_array1[4];
	email->isRead=F;
	email->isMarked=F;
	email->ID=88;
	email->hour=120000;
	email->date=20141103;
	strcpy(email->recipient,"NoReply@Phishing.ru");
	strcpy(email->sender,"student@tau.ac.il");
	strcpy(email->subject,"Pictures From Last Night");
	strcpy(email->content,"Hey Student, check out the pictures from last night!! Amazing!!\nClick HERE,\nLove You");

	//Email 6

	email=&emails_array2[0];
	email->isRead=F;
	email->isMarked=F;
	email->ID=10;
	email->hour=000001;
	email->date=19841205;
	strcpy(email->recipient," T-800Model101@Skynet.org");
	strcpy(email->sender,"TerminationAdministration@Skynet.org");
	strcpy(email->subject,"Target_Acquired");
	strcpy(email->content,"Sarah Conner\n Female, 19, 5'5, Blonde Hair\n\nTerminate ASAP");

	//Email 7

	email=&emails_array3[0];
	email->isRead=F;
	email->isMarked=F;
	email->ID=100;
	email->hour=120001;
	email->date=20141103;
	strcpy(email->recipient,"lecturer@tau.ac.il");
	strcpy(email->sender,"worried_mom@tau.ac.il");
	strcpy(email->subject,"Worried about my son");
	strcpy(email->content,"Hi lecturer, my son goes to your Infi 1 class. Ever since he seems depressed and he started wearing only black. Is this common behavior? \nThanks,\nWorried mom.");

	//Email 8

	email=&emails_array3[1];
	email->isRead=F;
	email->isMarked=F;
	email->ID=101;
	email->hour=120000;
	email->date=20151021;
	strcpy(email->recipient,"MartyMcfly@tau.ac.il");
	strcpy(email->sender,"DocBrown@tau.ac.il");
	strcpy(email->subject,"A message from the future");
	strcpy(email->content,"Marty, if you can read this that means my time machine worked.\nI gotta tell you Marty, the future is pretty awesome.\n I bet you're pretty sorry for staying home with your girlfriend.. Smell ya later chum");
	
	
	//Email 9

	email=&emails_array3[2];
	email->isRead=F;
	email->isMarked=F;
	email->ID=72;
	email->hour=140722;
	email->date=20141028;
	strcpy(email->recipient,"student@tau.ac.il");
	strcpy(email->sender,"VardaBotvinvik@tau.ac.il");
	strcpy(email->subject,"Greetings from the Dean");
	strcpy(email->content,"The Dean sends his greetings");
	
	
	//Email 10

	email=&emails_array3[3];
	email->isRead=F;
	email->isMarked=F;
	email->ID=79;
	email->hour=140922;
	email->date=20141028;
	strcpy(email->recipient,"student@tau.ac.il");
	strcpy(email->sender,"VardaBotvinvik@tau.ac.il");
	strcpy(email->subject,"FW: Job Offer");
	strcpy(email->content,"WePayLikeShit Inc. Company offers a job to years 3-4 students. \nCV's to WeAreNeverGoingToReadIt@WePayLikeShit.com");

	
	//Email 11

	email=&emails_array3[4];
	email->isRead=F;
	email->isMarked=F;
	email->ID=25;
	email->hour=141124;
	email->date=20141028;
	strcpy(email->recipient,"student@tau.ac.il");
	strcpy(email->sender,"VardaBotvinvik@tau.ac.il");
	strcpy(email->subject,"FW:Re:FW:Scholarship");
	strcpy(email->content,"Moshe's Fundation Offers 1000 NIS scholarship to a student willing to volunteer 50 hours a week");

	//Email 12

	email=&emails_array3[5];
	email->isRead=F;
	email->isMarked=F;
	email->ID=26;
	email->hour=141129;
	email->date=20141028;
	strcpy(email->recipient,"student@tau.ac.il");
	strcpy(email->sender,"VardaBotvinvik@tau.ac.il");
	strcpy(email->subject,"Semester in Venice");
	strcpy(email->content,"Can someone PLEASE accept this offer this one time? PLEASEEEEE");

	
	//Email 13

	email=&emails_array3[6];
	email->isRead=F;
	email->isMarked=F;
	email->ID=30;
	email->hour=174329;
	email->date=20141030;
	strcpy(email->recipient,"student@tau.ac.il");
	strcpy(email->sender,"BitachonSecretary@tau.ac.il");
	strcpy(email->subject,"Road works in gate 4");
	strcpy(email->content,"Fifth time this year, road works in gate 4, roads are closed.");
	return;
}

Email * Get_Emails_From_Server (int* number_of_emails)
{

	switch (current_access)
	{
		case 0:
			current_access++;
			*number_of_emails=5;
			return emails_array1;
			break;
		case 1:
			current_access++;
			*number_of_emails=1;
			return emails_array2;
			break;
		case 2:
			current_access++;
			*number_of_emails=0;
			return NULL;
			break;
		case 3:
			current_access++;
			*number_of_emails=7;
			return emails_array3;
			break;
		default:
			printf("Error! too many Server accesses!");
			return NULL;
	}
}

void Send_to_Server(Email* email)
{
	Sleep(200);
}

void Send_Email (Email* email)
{
	Send_to_Server(email);
	printf("Email Sent! Subject: %s",email->subject);
}

void Close_Server()
{
	//Ram 8_11_14 test entrance
	//printf("in module: %s function: %s\r\n", "HW1_Server.c", "Close_Server");
	//
//	int i;		RAM_8_11_14 WTF?!
		free(emails_array1);
		free(emails_array2);
		free(emails_array3);
	printf("Server Closed Successfully!\n");
	return;
}